/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Honorbuddy.QuestBehaviorCore;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.ALineInTheDirt
{
	[CustomBehaviorFileName(@"25045-ALineInTheDirt")]
	public class ALineInTheDirt : CustomForcedBehavior
	{
		public ALineInTheDirt(Dictionary<string, string> args)
			: base(args)
		{
		try
			{
				QuestId = 25045; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: A Line In The Dirt");
			}
		}

		public int QuestId { get; set; }
		public int MobIdNeutralFlag = 201743;
		public int MobIdAllyFlag = 202460;
		public int MobIdAttacker = 37706;
		private bool _isBehaviorDone;
		public static LocalPlayer me = StyxWoW.Me;
		
		public List<WoWUnit> Attacker
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => (u.Entry == MobIdAttacker && u.IsAlive))
									.OrderBy(u => u.Distance).ToList();
			}
		}
		
		public WoWUnit NFlag
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => (u.Entry == MobIdNeutralFlag))
									.OrderBy(u => u.Distance).FirstOrDefault();
			}
		}
		
		public WoWUnit AFlag
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => (u.Entry == MobIdAllyFlag))
									.OrderBy(u => u.Distance).FirstOrDefault();
			}
		}
		
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}
		
		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
		
		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				if (TreeRoot.Current != null && TreeRoot.Current.Root != null && TreeRoot.Current.Root.LastStatus != RunStatus.Running)
				{
					var currentRoot = TreeRoot.Current.Root;
					if (currentRoot is GroupComposite)
					{
						var root = (GroupComposite)currentRoot;
						root.InsertChild(0, CreateBehavior());
					}
				}
				PlayerQuest Quest = Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}
		
		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(context => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}
		
		public WoWUnit tar = null;
		
		public Composite Attack
		{
			get
			{
				return new PrioritySelector(
					new Action(context =>
					{
						TreeRoot.StatusText = "Defend the flag!";
						var flagattacker =
							ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => 
								r.Entry == MobIdAttacker && r.HasAura(70315) && r.IsAlive).OrderBy(r=>
								r.Distance).FirstOrDefault();
						var attacker =
							ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => 
								r.Entry == MobIdAttacker && r.IsAlive && r.CurrentTarget == Me).OrderBy(r=>
								r.Distance).FirstOrDefault();
						if (flagattacker != null)
						{
							tar = flagattacker;
							return RunStatus.Failure;
						}
						else if (attacker != null)
						{
							tar = attacker;
							return RunStatus.Failure;
						}
						else
						{
							Logging.Write("No viable targets, waiting.");
							return RunStatus.Failure;
						}
					}),
					new Decorator(context => tar != null,
						new Sequence(
							new Action(context =>
							{
								tar.Target();
								tar.Face();
							}),
							new UtilityBehaviorPS.MiniCombatRoutine())));
			}
		}

		private Composite _root;
		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(context => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => NFlag != null,
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet, Attack, new ActionAlwaysSucceed())));
		}
	}
}

